﻿function Get-TSMReplicationJob {    
    <#
        .SYNOPSIS
            Get TSM replication jobs
                    
        .DESCRIPTION
            The Get-TSMReplicationJob returns replication jobs from the TSM server

        .PARAMETER Node
            The node to search
            
        .PARAMETER StartDate
            The earliest start date of the replication job
            
        .PARAMETER EndDate
            The latest end date of the replication job

        .EXAMPLE
            Get-TSMReplicationJob

        .NOTES
            Contributors:  David Seibel
            Last Revision Date: 09/02/2015

    #>
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0)]
        $Node,
        
        [Parameter(Position = 1)]
        [DateTime]$StartDate = (Get-Date).AddDays(-1),

        [Parameter(Position = 2)]
        [DateTime]$EndDate
    )
    
    if ($StartDate -lt $EndDate) {
        $whereProps = @()
        $whereProps += [PSCustomObject]@{ "Column" = "NODE_NAME";  "Operator" = "LIKE"; "Value" = $Node }
        $whereProps += [PSCustomObject]@{ "Column" = "START_TIME"; "Operator" = ">=";   "Value" = $StartDate }
        $whereProps += [PSCustomObject]@{ "Column" = "END_TIME";   "Operator" = "<=";   "Value" = $EndDate }

        $query = Format-TSMQuery -Table "REPLICATIONVIEW" -Where $whereProps -OrderBy "NODE_NAME"
        $result = Invoke-TSMCommand -Command $query

        $defaultDisplaySet = 'NODE_NAME','START_TIME','END_TIME','COMP_STATE'
        $typeDefinitions = @{
                             "START_TIME" = [DateTime]
                             "END_TIME"   = [DateTime]
                            }

        return Format-TSMData -Data $result -TypeDefinitions $typeDefinitions -DefaultDisplaySet $defaultDisplaySet
    } else {
        Write-Warning "Start date must be before the end date!"
    }
}